<?php
	class CompanyManageAction extends Action{
		//企业管理首页
		public function company(){
			$enter=M('enterprise');	   //实例化企业表
			$keyword=$this->_post('keyword');		//获取企业名字
			if(!empty($keyword)){
				$where='Name like'."'%".$keyword."%'";	//组合查询语句
				$this->assign('keyword',$keyword);
			}else{
				$where="";
			}	
			if($_POST['numPerPage']){
				$pagesize = $this->_post('numPerPage');
				$_GET['page'] = $this->_post('pageNum');//自定义翻页类需要 GET （当前页）信息
			}else{
				$pagesize=10;
			}
			$totalCount=$enter->where($where)->count();
			$this->assign('totalCount',$totalCount);				//把总数发给模板
			import('@.Action.Page');
			$page = new Page($totalCount,$pagesize);				//实例化分页类
			$pageinfo = $page->getPageInfo();						//获取分页
			$this->assign('currentPage',$pageinfo['page']);			//分配当前页数
			$this->assign('pagesize',$pagesize);					//分配一页显示的条数
			$arr=$enter->field('Id,Name,Logo,Introduce,LinkMan,TelePhone,WebUrl,IsCo')->where($where)->limit($pageinfo['offset'],$pageinfo['num'])->select();//获取企业信息
			$this->assign('arr',$arr);				//传值给模板
			$this->display();
		}

		//编辑企业信息
		public function edit(){
			$id=$this->_get('cid');				//获取企业id
			$enter=M('enterprise');				//实例化企业表
			$list=$enter->field('Id,Name,Introduce,LinkMan,TelePhone,WebUrl,IsCo')->where('Id='.$id)->find();	//获取企业信息
			$this->assign('list',$list);			//传值给模板
			$this->display();
		}

		//处理编辑和添加企业信息
		public function upload(){
			import('ORG.Net.UploadFile');			//引用上传类
			$id=$this->_post('cid');				//获取id值
			$data['Name']=$this->_post('name');		//获取企业名字
			$data['Introduce']=$this->_post('introduce');	//获取企业简介
			$data['LinkMan']=$this->_post('linkman');		//获取联系人
			$data['TelePhone']=$this->_post('telephone');	//获取联系人电话
			$data['WebUrl']=$this->_post('weburl');			//获取企业网站
			$data['IsCo']=$this->_post('isco');				//获取是否协助单位
			if($data['IsCo']=="on"){					
				$data['IsCo']=1;
			}else{
				$data['IsCo']='0';
			}
			$upload = new UploadFile();									// 实例化上传类
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');	// 设置附件上传类型
			$upload->maxSize  = 31457280;// 设置附件上传大小
			$upload->savePath = './UpFile/Img/test/';					// 设置附件上传目录
			if(!$upload->upload()) {								// 上传错误提示错误信息
				$this->error_ajax($upload->getErrorMsg());
			}else{													// 上传成功 获取上传文件信息
				$enter=M('enterprise');
				$info =  $upload->getUploadFileInfo();				//获取图片信息
				$data['Logo']=$info[0]['savename'];					//获取图片名字
				$opath="./UpFile/Img/test/".$data['Logo'];			//上传路径
				$npath="./UpFile/Img/EnterpriseLogo";				//新路径
				doresize(120,$opath,$npath,$data['Logo'],50);			//调用函数移到新目录
				unlink('./UpFile/Img/test/'.$data['Logo']);			//删除原图片
				if($id == 0){										//id为0执行添加
					$res=$enter->add($data);					
				}else{
					$res=$enter->where('Id='.$id)->save($data);		//id为1执行修改
				}
				if($res){
					$logo=$enter->field('Logo')->where('Id = '.$id)->find();	//获取修改前图片名
					unlink('./UpFile/Img/EnterpriseLogo/'.$logo['Logo']);		//删除修改前图片
					$this->success_ajax('成功');
				}else{
					$this->error_ajax('失败');
				}
			}
		}
		//添加竞赛题目
		public function addproject(){
			$enter=M('enterprise');						//实例化企业表
			$arr=$enter->field('Id,Name')->select();	//获取企业名和id
			$this->assign('arr',$arr);					//输出给模板
			$this->display();
		}
		//处理添加竞赛题目
		public function do_addject(){
			$data['EnterpriseId']=$this->_post('term');				//获取企业id
			$da=M('enterprise')->where('Id='.$data['EnterpriseId'])->field('Name')->find();	//获取企业名字
			$data['CreateUser']=$da['Name'];					//获取企业名字
			$data['Title']=$this->_post('Title');				//获取标题
			$data['Goal']=$this->_post('Goal');					//获取目标
			$data['Description']=$this->_post('Description');	//获取描述
			$data['Support']=$this->_post('Support');			//获取支持
			$data['Advise']=$this->_post('Advise');				//获取建议
			$data['CreateDate']=date('Y-m-d H:i:s',time());		//获取创建时间
			$data['IsApproved']=0;								//未通过
			$data['IsPublic']=0;								//不公开
			$data['IsTop']=0;									//不置顶
			$bus=M('businessproblems');							//实例化竞赛题目表
			if($bus->add($data)){								//添加
				$this->success_ajax('添加成功');
			}else{
				$this->error_ajax('添加失败');
			}		
		}
		//审核首页
		public function passd(){
			$enter=M('enterprise');						//实例化企业表
			$model=M();				//实例化
			$keyword=$this->_post('keyword');			//获取标题名
			$term1=$this->_post('term1');				//获取企业id
			$term2=$this->_post('term2');				//获取时间值
			$where="";									
			if(!empty($keyword)){
				$where='b.Title like'."'%".$keyword."%' and ";	//组合查询条件
				$this->assign('keyword',$keyword);				
			}
			if($term1!=0){
				$where="b.EnterpriseId =".$term1." and ";	//组合查询条件
				$this->assign('keyword',$term1);
			}
			if($term2==0){
				$order="b.CreateDate desc";				//组合排序条件
				$this->assign('keyword',$term2);
			}else{
				$order="b.UpdateTiem desc";				//组合排序条件
				$this->assign('keyword',$term2);
			}
			if($_POST['numPerPage']){
				$pagesize = $this->_post('numPerPage');
				$_GET['page'] = $this->_post('pageNum');//自定义翻页类需要 GET （当前页）信息
			}else{
				$pagesize=20;
			}	
			$totalCount=$model->table('businessproblems b,enterprise e')->where($where.'b.EnterpriseId=e.Id')->count();
			$this->assign('totalCount',$totalCount);				//把总数发给模板
			import('@.Action.Page');
			$page = new Page($totalCount,$pagesize);				//实例化分页类
			$pageinfo = $page->getPageInfo();						//获取分页
			$this->assign('currentPage',$pageinfo['page']);			//分配当前页数
			$this->assign('pagesize',$pagesize);					//分配一页显示的条数
			$list=$model->table('businessproblems b,enterprise e')->field('b.IsTop,b.Id,b.Title,e.Name,b.IsPublic,b.IsApproved,b.ChoiceCount')->where($where.'b.EnterpriseId=e.Id')->order('b.IsTop desc,'.$order)->limit($pageinfo['offset'],$pageinfo['num'])->select();		//获取竞赛题目信息
			$this->assign('list',$list);
			$arr=$enter->field('Id,Name')->select();			//获取企业名
			$this->assign('arr',$arr);
			$this->display();
		}
		//为被审核首页
		public function unpassd(){
			$enter=M('enterprise');
			$model=M();
			$keyword=$this->_post('keyword');			//获取标题名
			$term1=$this->_post('term1');				//获取企业id
			$term2=$this->_post('term2');				//获取时间值
			$where="";								
			if(!empty($keyword)){
				$where='b.Title like'."'%".$keyword."%' and ";	//组合查询条件
				$this->assign('keyword',$keyword);				
			}
			if($term1!=0){
				$where="b.EnterpriseId =".$term1." and ";	//组合查询条件
				$this->assign('keyword',$term1);
			}
			if($term2==0){
				$order="b.CreateDate desc";				//组合排序条件
				$this->assign('keyword',$term2);
			}else{
				$order="b.UpdateTiem desc";				//组合排序条件
				$this->assign('keyword',$term2);
			}
			if($_POST['numPerPage']){
				$pagesize = $this->_post('numPerPage');
				$_GET['page'] = $this->_post('pageNum');//自定义翻页类需要 GET （当前页）信息
			}else{
				$pagesize=20;
			}	
			$totalCount=$model->table('businessproblems b,enterprise e')->where($where.'b.EnterpriseId=e.Id and b.IsApproved=0')->count();
			$this->assign('totalCount',$totalCount);				//把总数发给模板
			import('@.Action.Page');
			$page = new Page($totalCount,$pagesize);				//实例化分页类
			$pageinfo = $page->getPageInfo();						//获取分页
			$this->assign('currentPage',$pageinfo['page']);			//分配当前页数
			$this->assign('pagesize',$pagesize);					//分配一页显示的条数
			$list=$model->table('businessproblems b,enterprise e')->field('b.IsTop,b.Id,b.Title,e.Name,b.IsPublic,b.IsApproved,b.ChoiceCount')->where($where.'b.EnterpriseId=e.Id and b.IsApproved=0')->order('b.IsTop desc,'.$order)->limit($pageinfo['offset'],$pageinfo['num'])->select();	//获取竞赛题目信息
			$this->assign('list',$list);	
			$arr=$enter->field('Id,Name')->select();		//获取企业名字
			$this->assign('arr',$arr);
			$this->display();
		}
		//置顶和取消置顶
		public function top(){
			$bus=M('businessproblems');
			$id=$this->_get('cid');				//获取id
			$type=$this->_get('type');			//获取执行方法
			if($type==1){
				$data['IsTop']=0;				//取消置顶为0
			}else{
				$data['IsTop']=1;				//置顶为1
			}
			if($bus->where('Id='.$id)->save($data)){	//更新信息
				$this->success_ajax('置顶成功');
			}else{
				$this->error_ajax('置顶失败');
			}
		}
		//删除
		public function delete(){
			$id=$this->_get('cid');					//获取id
			$type=$this->_get('type');
			if($type==1){
				$bus=M('businessproblems');				//实例化竞赛题目表
				$bus->where('Id='.$id)->delete();
			}else{
				$enter=M('enterprise');
				$res=$enter->where('Id='.$id)->delete();
			}
			if($res){		//删除竞赛题目
				$this->success_ajax('删除成功');
			}else{
				$this->error_ajax('删除失败');
			}
		}
		//详细信息
		public function info(){
			$id=$this->_get('cid');					//获取企业id
			$enter=M('enterprise');					//实例化企业表
			$arr=$enter->field('Id,Name')->select();	//获取企业名
			$this->assign('arr',$arr);					//发给模板

			$model=M('businessproblems');			//实例化竞赛题目表
			$list=$model->table('businessproblems b,enterprise e')->field('b.Goal,b.Id,b.Title,e.Name,b.IsPublic,b.IsApproved,b.Description,b.Support,b.Advise')->where('b.EnterpriseId=e.Id and b.Id='.$id)->order('b.IsTop desc,b.CreateDate desc')->find();			//获取企业题目信息
			$this->assign('list',$list);
			$this->display();
		}
		//处理详细信息
		public function do_info(){
			$id=$this->_post('cid');							//获取id
			$data['EnterpriseId']=$this->_post('term');			//获取企业id
			$da=M('enterprise')->where('Id='.$data['EnterpriseId'])->field('Name')->find();	//获取企业名
			$data['CreateUser']=$da['Name'];				//获取企业名
			$data['Title']=$this->_post('Title');			//获取标题
			$data['Goal']=$this->_post('Goal');				//获取目标
			$data['Description']=$this->_post('Description');	//获取描述
			$data['Support']=$this->_post('Support');			//获取支持
			$data['Advise']=$this->_post('Advise');				//获取建议
			$data['UpdateTiem']=date('Y-m-d H:i:s',time());		//获取修改时间
			$data['IsApproved']=$this->_post('IsApproved');		//获取是否通过
			$data['IsPublic']=$this->_post('IsPublic');			//获取是否公开
			$bus=M('businessproblems');							//实例化题目表
			if($bus->where('Id='.$id)->save($data)){			//修改信息
				$this->success_ajax('修改成功');
			}else{
				$this->error_ajax('修改失败');
			}
		}
	}
